import gtk
import os, time

tab_icon_size=32
icon_size=128

def Refresh():
    os.system("gconftool-2 -u '/desktop/gnome/interface/icon_theme'")
    time.sleep(3)
    os.system("gconftool-2 -s '/desktop/gnome/interface/icon_theme' --type string hydroxygen")
              
def getPixbufFromName(iconName, size=icon_size, type="app"):
    #print iconName, size, type
    if isinstance(iconName ,gtk.gdk.Pixbuf)==False:
        icon_theme = gtk.icon_theme_get_default()
        pixbuf = None
        try:
            pixbuf = icon_theme.load_icon(iconName, size, 0)
            path = icon_theme.lookup_icon(iconName, size, 0).get_filename()
        except:
            if iconName and '/' in iconName:
                try:
                    pixbuf = gtk.gdk.pixbuf_new_from_file(iconName)
                    path = iconName
                except:
                    pass
            if pixbuf == None:
                if type=="folder":
                    iconName = 'gnome-fs-directory'
                elif type=="application" or type=="app":
                    iconName = 'application-default-icon'
                else:
                    iconName = 'text-x-generic'
                try:
                    pixbuf = gtk.gdk.pixbuf_new_from_file(iconName)
                    path = iconName
                except:
                    try:
                        pixbuf = icon_theme.load_icon(iconName, size, 0)
                        path = icon_theme.lookup_icon(iconName, size, 0).get_filename()
                    except:
                        pixbuf==None
                        pass
    else:
        pixbuf=iconName
    if pixbuf == None:
        print "cannot find icon:" + iconName
        return None
    if pixbuf.get_width() != size or pixbuf.get_height() != size:
        width=pixbuf.get_width()
        height=pixbuf.get_height()
        if width==height:
            width=size
            height=size
        else:    
            #if width > size:
            #    height=height*size/width
            #    width=size
                if height > size:
                    width=width*size/height
                    height=size
        pixbuf = pixbuf.scale_simple(width, height, gtk.gdk.INTERP_HYPER)
    return pixbuf

def scale_pixbuf(pixbuf, size):
    width=pixbuf.get_width()
    height=pixbuf.get_height()
    if width==height:
        width=size
        height=size
    else:    
        if width > size:
            height=height*size/width
            width=size
            if height > size:
                width=width*size/height
                height=size
    pixbuf = pixbuf.scale_simple(width, height, gtk.gdk.INTERP_HYPER)
    return pixbuf

def read_options():
    list=[]
    file=open(os.path.expanduser("~")+"/.icons/hydroxygen/options.csv", "r")
    for line in file.readlines():
        line=line.replace("\n", "")
        line=line.replace('"', "")
        if line.find(";")>=0:
            sep=";"
        else:
            sep=","
        line=line.replace(sep+sep, sep)
        line=line.split(sep)
        i=0
        while i<len(line):
            if line[i]=="": 
                line.pop(i)
            else: i+=1
        if len(line)>=3:
            name=line.pop(0)
            icon=line[0]
            list.append({"name":name, "icon":icon, "option-list":line, "choice":icon})
    return list

def read_last_options():
    try:
        list=[]
        file=open(os.path.expanduser("~")+"/.hydroxygenrc", "r")
        for line in file.readlines():
            line.split()
            line=line.replace("\n", "")
            line=line.replace('"', "")
            list.append(line)
        return list
    except IOError:
        return None
